package tp.badugi.bot.guiViewsBot;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;

import tp.badugi.bot.GuiClientWithBot;

public class ServerViewBot extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8367090872919498693L;

	private GuiClientWithBot client;
	
	private final Dimension serverViewSize = new Dimension(500, 300);
	
	private JLabel validationMessager = new JLabel();

	public JList<String> serverList = new JList<String>();;
	private JScrollPane serverListScroll = new JScrollPane();
	
	private JTabbedPane serverTabPane = new JTabbedPane();
	
	private JPanel serverJoinTablePanel = new JPanel();
	private JPanel serverCreateTablePanel = new JPanel();
	private JPanel serverTableListPanel = new JPanel();
	private JPanel serverTableInfoPanel = new JPanel();
	private JPanel serverTableInfoButtonPanel = new JPanel();
		
	private JLabel serverNameAndPort = new JLabel();
	
	private JTextArea serverTableInfoTextArea = new JTextArea("Click on table name to display info");
	
	public JButton serverJoinTable = new JButton("Join table");
	public JButton serverRefresh = new JButton("Refresh");
	public JButton serverDisconnect = new JButton("Disconnect");
	public JButton serverCreate = new JButton("Create table");
	public JButton serverBotOn = new JButton("Bot OFF");
	
	public JTextField serverTableNameTextField = new JTextField("Table name");
	public JTextField serverPlayerNameTextField = new JTextField("Your nickname");
	public JTextField serverTableSizeTextField = new JTextField("Table size");
	public JTextField serverStartingChipsTextField = new JTextField("Starting chips");
	public JTextField serverSmallBlind = new JTextField("Small blind");
	public JTextField serverBigBlind = new JTextField("Small blind");
	
	public ServerViewBot(GuiClientWithBot client,String serverIpAddress, int serverPortNumber,
			String[] serverList) {
		
		this.client = client;
		
		this.client.defaultButtonColor = this.serverBigBlind.getBackground();
		
		this.makeView();
		this.refreshView(serverIpAddress, serverPortNumber, serverList);
	}
	
	private void makeView(){
		
		this.setTitle(this.client.title);

		this.setBounds(new java.awt.Rectangle(this.client.windowBoundPoint, this.serverViewSize));
		this.setFont(this.client.clientFont);
		
		this.setLayout(new BorderLayout());
		
		this.serverJoinTablePanel.setLayout(new BorderLayout());
		this.serverTabPane.addTab("Join table", this.serverJoinTablePanel);
		this.serverCreateTablePanel.setLayout(new GridLayout(7,1));
		this.serverTabPane.addTab("Create table", this.serverCreateTablePanel);
		
		this.serverTableListPanel.setLayout(new BorderLayout());
		this.serverJoinTablePanel.add(this.serverTableListPanel, BorderLayout.CENTER);
		this.serverTableInfoPanel.setLayout(new BorderLayout());
		this.serverJoinTablePanel.add(this.serverTableInfoPanel, BorderLayout.EAST);
		
		this.serverNameAndPort.setHorizontalAlignment(SwingConstants.CENTER);
		
		this.add(this.serverTabPane, BorderLayout.CENTER);
		this.add(this.validationMessager, BorderLayout.SOUTH);
		this.add(this.serverNameAndPort, BorderLayout.NORTH);
		
		this.serverList.setLayoutOrientation(JList.VERTICAL);
		this.serverList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		this.serverListScroll.setViewportView(this.serverList);
		
		this.serverTableListPanel.add(this.serverListScroll, BorderLayout.CENTER);
		this.serverTableListPanel.add(this.serverRefresh, BorderLayout.SOUTH);
		this.serverRefresh.addActionListener(this.client);
		
		this.serverTableInfoTextArea.setEditable(false);
		this.serverTableInfoTextArea.setHighlighter(null);
		this.serverTableInfoTextArea.setLineWrap(true);
		this.serverTableInfoPanel.add(this.serverTableInfoTextArea, BorderLayout.CENTER);
		this.serverTableInfoButtonPanel.setLayout(new GridLayout(3,1));
		this.serverTableInfoButtonPanel.add(this.serverJoinTable);
		this.serverJoinTable.addActionListener(this.client);
		this.serverTableInfoButtonPanel.add(this.serverDisconnect);
		this.serverDisconnect.addActionListener(this.client);
		this.serverTableInfoButtonPanel.add(this.serverBotOn);
		this.serverBotOn.addActionListener(this.client);
		this.serverTableInfoPanel.add(this.serverTableInfoButtonPanel, BorderLayout.SOUTH);
		
		this.serverCreateTablePanel.add(this.serverTableNameTextField);
		this.serverCreateTablePanel.add(this.serverPlayerNameTextField);
		this.serverCreateTablePanel.add(this.serverTableSizeTextField);
		this.serverCreateTablePanel.add(this.serverStartingChipsTextField);
		this.serverCreateTablePanel.add(this.serverSmallBlind);
		this.serverCreateTablePanel.add(this.serverBigBlind);
		this.serverCreateTablePanel.add(this.serverCreate);
		this.serverCreate.addActionListener(this.client);

		this.setVisible(true);
		this.setResizable(false);
		this.addWindowListener(this.client.windowAdapter);
	}
	
	public void refreshView(String serverIpAddress, int serverPortNumber,
			String[] serverList){
		this.serverNameAndPort.setText(serverIpAddress + ":" + serverPortNumber);
		
		this.serverList.setListData(serverList);
		
		if(this.client.botOn){
			this.serverBotOn.setBackground(Color.GREEN);
			this.serverBotOn.setText("Bot ON");
		}
		else{
			this.serverBotOn.setBackground(this.client.defaultButtonColor);
			this.serverBotOn.setText("Bot OFF");
		}
	}
}
